package com.lg.erp.service.impl.zhangchaofu;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lg.erp.entity.zhangchaofu.*;
import com.lg.erp.mapper.zhangchaofu.MoveWarehouseMapper;
import com.lg.erp.service.zhangchaofu.MoveWarehouseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.UUID;

@Service
public class MoveWarehouseServiceImpl extends ServiceImpl<MoveWarehouseMapper, TMoveWarehouse> implements MoveWarehouseService {
    @Autowired
    private MoveWarehouseMapper moveWarehouseMapper;

    @Override
    public IPage<TMoveWarehouse> findMoveWarehousePage(Page<TMoveWarehouse> page) {
        return moveWarehouseMapper.findMoveWarehousePage(page);
    }

    @Override
    public List<TMoveWarehouse> warehouseDetails(TMoveWarehouse tMoveWarehouse) {
        return moveWarehouseMapper.warehouseDetails(tMoveWarehouse);
    }

    @Override
    public List<ZCFTWare> findAllWare() {
        return moveWarehouseMapper.findAllWare();
    }

    @Override
    public List<TMoveWarehouse> findCargo(TMoveWarehouse warehouse) {
        return moveWarehouseMapper.findCargo(warehouse);
    }

    @Override
    public List<ZCFTWare> findWareCargoAreaShelveCargo(String wareId) {
        return moveWarehouseMapper.findWareCargoAreaShelveCargo(wareId);
    }


    @Override
    public TIomWarehouse addIomWarehouseOutWare(TIomWarehouse iomWarehouse) {
        //出库
        Date now = new Date();
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd");//可以方便地修改日期格式
        String time = dateFormat.format(now);
        java.sql.Date tim = java.sql.Date.valueOf(time.replace("/", "-"));
        iomWarehouse.setIomTime(tim);
        String id = UUID.randomUUID().toString().replace("-", "").substring(0, 20);
        String iomNo = UUID.randomUUID().toString().replace("-", "").substring(0, 20);
        iomWarehouse.setIomNo(iomNo);
        iomWarehouse.setId(id);
        //添加出库信息
        moveWarehouseMapper.addIomWarehouseOutWareAndInWare(iomWarehouse);
        //删除货位表上的商品
        moveWarehouseMapper.moveCargoProduct(iomWarehouse.getCargoId());
        return iomWarehouse;
    }

    @Override
    public TIomWarehouse addIomWarehouseInWare(TIomWarehouse iomWarehouse) {
        Date now = new Date();
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd");//可以方便地修改日期格式
        String time = dateFormat.format(now);
        java.sql.Date tim = java.sql.Date.valueOf(time.replace("/", "-"));
        iomWarehouse.setIomTime(tim);
        String id = UUID.randomUUID().toString().replace("-", "").substring(0, 20);
        iomWarehouse.setId(id);
        //根据货区编号查询货区信息
        ZCFTCargoArea cargoArea= moveWarehouseMapper.findCargoArea(iomWarehouse.getIomArea());
        iomWarehouse.setIomArea(cargoArea.getGoodsName());
        moveWarehouseMapper.addIomWarehouseOutWareAndInWare(iomWarehouse);
        //添加商品到指定的货位
        ZCFTShelves shelveCargo = moveWarehouseMapper.findShelveCargo(iomWarehouse.getIomShelves());
        ZCFTCargo cargo = new ZCFTCargo();
        cargo.setShelvesId(iomWarehouse.getIomShelves());
        cargo.setDamId(shelveCargo.getDamId());
        cargo.setWareId(shelveCargo.getWareId());
        cargo.setCargoNo(iomWarehouse.getIomCargoloca());
        cargo.setProductNo(iomWarehouse.getIomGoodsno());
        cargo.setProduct(iomWarehouse.getIomGoods());
        //入库更新货位
        moveWarehouseMapper.iomWarehouseUpdateCargo(cargo);
        return iomWarehouse;
    }

    @Override
    public void addMoveWarehouse(TMoveWarehouse moveWarehouse) {
        //查询仓库的名字
        ZCFTWare ware = moveWarehouseMapper.findWareName(moveWarehouse.getMvOut());
        moveWarehouse.setMvOut(ware.getWareName());
        String id = UUID.randomUUID().toString().replace("-", "").substring(0, 20);
        moveWarehouse.setId(id);
        moveWarehouseMapper.insert(moveWarehouse);
    }
}
